
require("config")
require("framework.init")
require("framework.shortcodes")
require("framework.cc.init")

local GameScene  = class("GameScene", function()
    return display.newScene("GameScene")
end)


local TAG_TMXMAP       = 1
local TAG_SPBODY      = 11
local TAG_SPBODY2     = 12

function GameScene:ctor()
  --local backColor = display.newColorLayer(ccc4(20,5,40,128)):addTo(self)
  local tmxMap = CCTMXTiledMap:create('myTiled.tmx'):addTo(self)
  tmxMap:setTag(TAG_TMXMAP)
  
  local item = ui.newImageMenuItem({image='CloseNormal.png',imageSelected='CloseSelected.png',listener=onClicked,tag=1})
  local menu = ui.newMenu({item}):addTo(self)
  menu:pos(display.width-40,display.height-40)
  menu:setZOrder(2)

  --self:addNodeEventListener(cc.NODE_ENTER_FRAME_EVENT, function (dt) self:onEnterFrame(dt) end)       --帧定时器，用于删除body,sprite
  --self:scheduleUpdate()

  local touch = display.newLayer():addTo(self)
  touch:setTouchEnabled(true)
  touch:addNodeEventListener(cc.NODE_TOUCH_EVENT, onTouch)

end

function onClicked(tag)
  CCLuaLog('... onClicked ...')
  local tmx = display.getRunningScene():getChildByTag(TAG_TMXMAP)
  local array = tmx:getObjectGroups()
  local tmxProperties = tmx:getProperties()
  local count = array:count()
  printf('array have %d counts!',count)
  for i=0,count-1 do
    local g = array:objectAtIndex(i)
    local name = g:getGroupName()
    printf('the %d group name is %s',i,name)
  end

  count = tmxProperties:count()
  printf('tmx have %d tmxProperties!',count)


  local g1 = array:objectAtIndex(0)
  local dic = g1:getProperties()
  local objects = g1:getObjects()
  count = dic:count()
  printf('dic have %d counts!',count)
  count = objects:count()
  printf('objects have %d counts!',count)

  dic = g1:objectNamed('o1')
  if not dic then CCLuaLog('not have 01') else CCLuaLog('... have o1 ...') end
  printf('dic key \'type is\' %s !',dic:valueForKey('type'):getCString())
  --printf('dic key \'visible is\' %s !',tostring(dic:valueForKey('visible'):boolValue()))
  printf('dic key \'name is\' %s !',dic:valueForKey('name'):getCString())
  printf('dic key \'gid is\' %d !',dic:valueForKey('gid'):uintValue())
  printf('dic key \'h is\' %d !',dic:valueForKey('h'):uintValue())
  printf('dic key \'_name is\' %s !',dic:valueForKey('_name'):getCString())
  --local dic2 = dic:objectForKey('properties')
  local dir2 = g1:objectNamed('properties')
  if not dic2 then CCLuaLog('not have dic2') else CCLuaLog('... have dic2 ...') end

  local g2 = array:objectAtIndex(1)
  local g2ObjectArray = g2:getObjects()
  CCLuaLog('... print all g2 Object name ...')
  for i=0,g2ObjectArray:count()-1 do
    local obj = g2ObjectArray:objectAtIndex(i)
    printf('%d obj name = %s',i+1,obj:valueForKey('name'):getCString())
  end
  --dic:writeToFile('export.txt')


  local myTMXLayer = tmx:layerNamed('myLayer01')
  local myTMXLayer2 = tmx:layerNamed('myLayer02')
  myTMXLayer2:hide()                                                          --隐藏第二层
  if not myTMXLayer then CCLuaLog('not have myTMXLayer') else CCLuaLog('... have myTMXLayer ...') end
  local size = myTMXLayer:getLayerSize()                                      --获取长宽的图块数量
  printf('myTMXLayer layer size is w=%g,h=%g',size.width,size.height)
  size = myTMXLayer:getMapTileSize()                                          --获取单个图块的尺寸
  printf('myTMXLayer map size is w=%g,h=%g',size.width,size.height)

  dic = myTMXLayer:getProperties()
  if not dic then CCLuaLog('not have dic') else CCLuaLog('... have dic ...') end
  --[[ dic为空的CCdictionary why????????????
  printf('myTMXLayer Properties key \'name is\' %s !',dic:valueForKey('layersname'):getCString())
  printf('myTMXLayer Properties key \'type is\' %s !',dic:valueForKey('type'):getCString())
  printf('myTMXLayer Properties key \'height is\' %d !',dic:valueForKey('height'):intValue())
  dic:writeToFile('export.txt')
  ]]

  local titleX,titleY = math.random(1,18),math.random(1,28)
  printf('titleX=%d , titleY=%d',titleX,titleY)
  local p = myTMXLayer:positionAt(CCPoint(titleX,titleY))                     --将title坐标转换成节点坐标
  printf('p x=%d , y=%d',p.x,p.y)
  local currentTiledId = myTMXLayer:tileGIDAt(CCPoint(titleX,titleY))         --获取图块ID
  local tmpTiledId = myTMXLayer:tileGIDAt(CCPoint(titleX-1,titleY))           --判断四个方向的图块，如果ID不一样，都将其转换成当前图块ID
  if currentTiledId ~= tmpTiledId then myTMXLayer:setTileGID(currentTiledId, CCPoint(titleX-1,titleY)) end
  tmpTiledId = myTMXLayer:tileGIDAt(CCPoint(titleX+1,titleY))
  if currentTiledId ~= tmpTiledId then myTMXLayer:setTileGID(currentTiledId, CCPoint(titleX+1,titleY)) end
  tmpTiledId = myTMXLayer:tileGIDAt(CCPoint(titleX,titleY-1))
  if currentTiledId ~= tmpTiledId then myTMXLayer:setTileGID(currentTiledId, CCPoint(titleX,titleY-1)) end
  tmpTiledId = myTMXLayer:tileGIDAt(CCPoint(titleX,titleY+1))
  if currentTiledId ~= tmpTiledId then myTMXLayer:setTileGID(currentTiledId, CCPoint(titleX,titleY+1)) end

  myTMXLayer:removeTileAt(CCPoint(titleX,titleY))                             --移除坐标位置图块

  return false
end


function onTouch(event)
  if event.name == 'began' then
    CCLuaLog('... onTouch ...')
  elseif event.name == 'moved' then
    local dx = event.x - event.prevX
    local dy = event.y - event.prevY
    local tmx = display.getRunningScene():getChildByTag(TAG_TMXMAP)
    local x = tmx:getPositionX() + dx
    local y = tmx:getPositionY() + dy
    tmx:pos(x,y)
  end

 
  return true
 
end


function GameScene:onEnter()
	CCLuaLog('GameScene:onEnter')																				--启用定时器监听
end


function GameScene:enterScene(sceneName, args, transitionType, time, more)
	 print('GameScene:enterScene..................')
end

function GameScene:onEnterFrame(dt)
  
end

game = {}

function game.startup()
    CCFileUtils:sharedFileUtils():addSearchPath("res/")
    print('startup..................')
    game.enterMainScene()
end

function game.exit()
    CCDirector:sharedDirector():endToLua()
    print('exit..........')
end

function game.enterMainScene()
	print('GameScene.new',type(GameScene.new))
    display.replaceScene(GameScene.new(), "fade", 0.6, display.COLOR_WHITE)
end
return game